/*
===============================================================================
                           COPYRIGHT NOTICE

   Copyright (C) 2004-2005 Daniel L. Heater
   International Copyright Secured.  All Rights Reserved.

-------------------------------------------------------------------------------

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  o Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
  o Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  o The authors name may be used to endorse or promote products derived from
    this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

===============================================================================
$Id: byteswap.h 13 2010-11-15 19:28:30Z dheater@heatertech.com $
===============================================================================
*/
#ifndef BYTESWAP_H
#define BYTESWAP_H

#ifdef __vxworks
#include <types/vxTypesBase.h>
#include <types/vxTypes.h>
#else
#include <stdint.h>
#endif

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef __PPC__
   static __inline__ uint16_t byteswap16( uint16_t x )
   {
      uint16_t ret;

      __asm__( "rlwimi %0, %2, 8, 16, 23" :
               "=&r" ( ret ) : "0" ( x >> 8 ), "r" ( x ) );

      return ret;
   }

   static __inline__ uint32_t byteswap32( uint32_t x )
   {
      uint32_t ret;

      __asm__( "rlwimi %0, %2, 24, 16, 23" :
               "=&r" ( ret ) : "0" ( x >> 24 ), "r" ( x ) );
      __asm__( "rlwimi %0, %2, 8, 8, 15" :
               "=&r" ( ret ) : "0" ( ret ), "r" ( x ) );
      __asm__( "rlwimi %0, %2, 24, 0, 7" :
               "=&r" ( ret ) : "0" ( ret ), "r" ( x ) );

      return ret;
   }
#else
#define byteswap16(x)  ((((uint16_t)(x) & 0xff00) >> 8) | \
                        (((uint16_t)(x) & 0x00ff) << 8))
#define byteswap32(x)  ((((uint32_t)(x) & 0xff000000) >> 24) | \
                        (((uint32_t)(x) & 0x00ff0000) >> 8) | \
                        (((uint32_t)(x) & 0x0000ff00) << 8) | \
                        (((uint32_t)(x) & 0x000000ff) << 24))
#endif

   static __inline__ uint64_t byteswap64( uint64_t x )
   {
      uint32_t b = ( x << 32 ) >> 32;
      uint32_t t = x >> 32;

      return ( ( uint64_t ) byteswap32( b ) << 32 ) | byteswap32( t );
   }

   static __inline__ int isbigendian()
   {
      static unsigned char endian_test[ 2 ] = { 1, 0 };

      return 1 != *( uint16_t* ) endian_test;
   }

#ifdef __cplusplus
}
#endif
#endif  /* BYTESWAP_H */
